package com.api.controller

import com.base.db.mysql.*
import com.base.db.redis.cache_getCode_pic
import com.base.db.redis.cache_getToken_refresh
import com.base.model.api.APIFrom
import com.base.model.api.AdminPageModel

class Controller_API_Admin private constructor() {
    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { Controller_API_Admin() }
    }

    //线路心跳
    fun Controller_API.ADMIN_00A01(params: Map<*, *>) = yResult()

    //刷新sign
    fun Controller_API.ADMIN_01A00(params: Map<*, *>): String {
        val refreshToken = _getRefreshToken()
        if (refreshToken.isNullOrBlank()) return yResult(code = -1, message = "签名不合法")
        val uid = redisService.cache_getToken_refresh(APIFrom.App, refreshToken)
        if (uid.isNullOrBlank()) return yResult(code = -1, message = "签名过期")
        return yResult(result = listOf(_getUserModelByUID(uid, APIFrom.App)))
    }

    //登录
    fun Controller_API.ADMIN_01A01(params: Map<*, *>): String {
        val password = params["password"]?.toString()
        val loginname = params["loginname"]?.toString()
        val code = params["code"]?.toString()
        if (password.isNullOrEmpty() || loginname.isNullOrEmpty() || code.isNullOrEmpty()) return yResult(code = -1, message = "数据不合法")
        if (password.length < 6) return yResult(code = -1, message = "密码至少6位")
        if (redisService.cache_getCode_pic(this, loginname) != code.toLowerCase()) return yResult(code = -1, message = "验证码错误")
        val uid = mysqlService.admin_getUserByAccount(loginname, encodePassword(password))
        if (uid.isNullOrEmpty()) return yResult(code = -1, message = "密码错误")
        return yResult(result = listOf(_getUserModelByUID(uid, APIFrom.Admin)))
    }

    //分页获取商品列表
    fun Controller_API.ADMIN_02A01(params: Map<*, *>): String {
        val page = params["page"]?.toString()?.toInt() ?: 1
        val pageSize = params["pageSize"]?.toString()?.toInt() ?: 10
        val searchContent = params["searchContent"]?.toString()
        val result = AdminPageModel<Map<String, Any>>()
        result.total = mysqlService.admin_getSPUTotal(searchContent)
        result.items = mysqlService.admin_getSPUs(page, pageSize, searchContent)
        return yResult(result = listOf(result))
    }

    //新增/编辑SPU
    fun Controller_API.ADMIN_02A02(params: Map<*, *>): String {
        val id = params["id"]?.toString()
        val title = params["title"]?.toString()
        val img_main = params["img_main"]?.toString()
        val video = params["video"]?.toString()
        val img_banner = params["img_banner"]?.toString()
        val img_detail = params["img_detail"]?.toString()
        val des = params["des"]?.toString()
        val content_like = params["content_like"]?.toString()
        val pinpai = params["pinpai"]?.toString()
        val xinghao = params["xinghao"]?.toString()
        val tuihuan = params["tuihuan"]?.toString()
        if (id.isNullOrEmpty()) {
            if (title.isNullOrEmpty() || img_main.isNullOrEmpty() || tuihuan.isNullOrEmpty()) return yResult(code = -1, message = "数据不合法")
            if (!mysqlService.admin_addSPU(
                            title,
                            img_main,
                            video,
                            img_banner,
                            img_detail,
                            des,
                            content_like,
                            pinpai,
                            xinghao,
                            tuihuan,
                    )) return yResult(code = -1, message = "新增失败")
        } else {
            if (title.isNullOrEmpty() || tuihuan.isNullOrEmpty()) return yResult(code = -1, message = "数据不合法")
            if (!mysqlService.admin_updateSPU(
                            id,
                            title,
                            des,
                            content_like,
                            pinpai,
                            xinghao,
                            tuihuan,
                    )) return yResult(code = -1, message = "更新失败")
        }
        return yResult()
    }

    //删除SPU
    fun Controller_API.ADMIN_02A03(params: Map<*, *>): String {
        val id = params["id"]?.toString()
        if (id.isNullOrEmpty()) return yResult(code = -1, message = "数据不合法")
        if (!mysqlService.admin_deleteSPU(id)) return yResult(code = -1, message = "数据删除失败")
        return yResult()
    }
}
